﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class LanguageEndPoints
    {
        private const string LanguagesUrl = "api/languages";
        private HttpClient client;

        public LanguageEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<LanguageViewModel>> GetAllLanguagesAsync()
        {
            List<LanguageViewModel> languages = new List<LanguageViewModel>();
            HttpResponseMessage response = await client.GetAsync(LanguagesUrl);
            if (response.IsSuccessStatusCode)
            {
                languages = await response.Content.ReadAsAsync<List<LanguageViewModel>>();
            }
            return languages;
        }

        public async Task<LanguageViewModel> GetLanguageAsync(int id)
        {
            LanguageViewModel language = null;
            HttpResponseMessage response = await client.GetAsync(LanguagesUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                language = await response.Content.ReadAsAsync<LanguageViewModel>();
            }
            return language;
        }

        public async Task CreateLanguageAsync(LanguageViewModel language)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(LanguagesUrl, language);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateLanguageAsync(LanguageViewModel language)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(LanguagesUrl, language);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteLanguageAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(LanguagesUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }

    }
}
